import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'enum'})
export class EnumPipe implements PipeTransform {

  transform(key: number, enums: Array<any>): string {
    let text = '';

    if (enums) {
      if (this.hasProp(enums[0], 'key')) {
        let item = enums.find(x => x.key == key);
        text = item ? item.value : '';
      } else {
        let item = enums.find(x => x.value == key);
        text = item ? item.text : '';
      }

    }

    return text;
  }

  hasProp (obj, prop) {
    return Object.prototype.hasOwnProperty.call(obj, prop);
  }
}
